var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var QQMapWX = require('../../utils/qqmap-wx-jssdk.min.js');
const app=getApp()

Page({
  /**
   * 页面的初始数据
   */
  data: {
    address:"定位中...",
    scrollDown: false,
    indexlists:{},
    banners:{},
    SantaClaus:'',
    txtAds:{},
    shopList:{},
    adverImg:"/img/index/horn.gif",
    wei:'',
    jing:''

  },
  clickMore:function(){
    wx.navigateTo({
      url: 'more/more'
    })
  },
  tapSearch:function(){
    wx.navigateTo({
      url: 'search/search'
    })
  },
  /*带经纬度轮播*/
  indexBanner: function () {
    let that = this
    util.request(api.IndexLocalBanner,{
      wei: wx.getStorageSync('wei'),
      jing: wx.getStorageSync('jing')
    },'POST').then(function(res){

      if (res.data.code == 200){
        that.setData({
          banners: res.data.data.banner,
          SantaClaus: res.data.data.foot_ads,
          txtAds: res.data.data.text_ads
        })
      }
      
    })
    
  },
  //商家分类
  indexlist:function(){
    let that = this
    util.request(api.IndexListUrl).then(function (res) {
      if (res.data.code == 200) {
        that.setData({
          indexlists: res.data.data,
        });
      }
    });
  },
  //店铺推荐
  indexshopList:function() {
    let that = this
    util.request(api.IndexShopListUrl, {

      wei: wx.getStorageSync('wei'),
      jing: wx.getStorageSync('jing')

    }, 'POST').then(function (res) {

      if(res.data.code == 200){
        that.setData({
          shopList: res.data.data,
        })
      }
      
    })
     
  },
  //封装openSetting
  openLocation :function(){
    //定位  获取用户位置信息
    var demo = new QQMapWX({   // 实例化API核心类
      key: 'SZSBZ-LFV33-YHK3A-YZZ77-YCVAQ-V4BYJ' // 必填
    });
    var that = this;
    wx.getLocation({
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.setStorageSync('wei', res.latitude)
        wx.setStorageSync('jing', res.longitude)
        that.indexBanner();
        that.indexlist();
        that.indexshopList();
        // 调用接口
        demo.reverseGeocoder({
          location: {
            latitude: latitude,
            longitude: longitude
          },
          success: function (res) {
            that.setData({
              address: res.result.address
            })
            wx.setStorageSync('add', res.result.address)
          }
        });
      },
      fail: function () {
        console.log("定位失败");
        that.failTosuccess();
      }
    })
     
  },

  //定位授权失败封装
  failTosuccess: function () {
    var that = this;
    wx.openSetting({
      success: function (res) {
        console.log(res)
        if (res.authSetting["scope.userLocation"]) {
          console.log("已打开位置权限")
          that.openLocation()
        } else {
          console.log("打开权限再次失败")
          that.failTosuccess()
        }
      }
    })
  },

  //监听页面加载
  onLoad: function (options) {
    this.openLocation()
  },

  //顶部滚动展示
  onScroll: function (e) {
    if (e.detail.scrollTop > 110 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 110 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
  }


})